package FeedMaker;

import java.io.File;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.os.Environment;

public class TorrentFeeder
{
	static public final String _file_name = "ddradio_torrent.xml";
	
	public void update( List< DdanjiEntry > entrys, String feed_title, String save_path )
	{
		String filename = feed_title + " torrent.xml";
		String filePath = Environment.getExternalStorageDirectory() + "/" + save_path + filename;
		
		String link_path = "/" + DdanjiEntry.toUTF8( feed_title ) + "/" + DdanjiEntry.toUTF8( filename );
		
		save_xml( filePath, entrys, feed_title, DropBoxHandler.inst().getLink( link_path ) );
		
		DropBoxHandler.inst().upload(filePath, link_path );
	}
	
	private void save_xml( String filename, List< DdanjiEntry > entrys, String feed_title, String xml_link )
	{
		XmlWriter writer = new XmlWriter();
		
		Document doc = writer.doc_;
		
		Element rss = doc.createElement( "rss" );
				
		doc.appendChild(rss);		
		{
			rss.setAttribute("version", "2.0");
			{
				Element channel = doc.createElement("channel");
				
				rss.appendChild(channel);				
				{
					channel.appendChild( create_text_elem( doc, "title", feed_title ) );					
					channel.appendChild( create_text_elem( doc, "language", "ko-kr" ) );
					channel.appendChild( create_text_elem( doc, "link", "http://www.ddanzi.com" ) );
					channel.appendChild( create_text_elem( doc, "description", "ddanji radio torrent feed" ) );
					channel.appendChild( create_text_elem( doc, "copyright", "all copyright reserved at ddanzi.com" ) );
					channel.appendChild( create_text_elem( doc, "generator", "ddanji torrent feed maker" ) );

					
					for( DdanjiEntry entry : entrys )
					{
						if( entry.IsUploaded() )
						{
							Element item = doc.createElement("item");
							{
								
								try {									
									String url = DropBoxHandler.inst().getLink( "/" + DdanjiEntry.toUTF8(feed_title) + "/" + DdanjiEntry.toUTF8( entry.title_ ) + ".torrent" );
									
									item.appendChild( create_text_elem( doc, "title", entry.title_ ) );
									item.appendChild( create_text_elem( doc, "link", url ) );
									
									Element enclosure = doc.createElement("enclosure");
									enclosure.setAttribute("url", url );									
									enclosure.setAttribute("length", Integer.toString( entry.torrent_size_ ) );
									enclosure.setAttribute("type", "application/x-bittorrent");
									item.appendChild(enclosure);
									
									Element guid = doc.createElement("guid");
									guid.setAttribute("isPermaLink" , "true" );
									guid.setTextContent(url);																		
									item.appendChild( guid );
									
									item.appendChild( create_text_elem( doc, "pubDate", entry.pubDate_ ) );									
									String description = "<![CDATA[" + entry.title_ + "<a href=\"" + url + "\">dl</a>]]";
									item.appendChild( create_text_elem( doc, "description", description ) );
								}
								catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								
							}
							
							channel.appendChild(item);	
						}
					}
				}				
			}
		}
		
		File file = new File(filename);
		
		if( file.isFile() )
			file.delete();
		
		writer.save( filename );
	}
	
	private Element create_text_elem( Document doc, String name, String textContent )
	{
		Element elem = doc.createElement( name );
		elem.setTextContent(textContent);
		return elem;
	}
}
